package com.czy.blog.entity;

import java.io.Serializable;
import java.util.HashSet;
import java.util.Set;

import javax.persistence.Access;
import javax.persistence.AccessType;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;

import org.hibernate.annotations.GenericGenerator;

@Entity
@Table(name = "sys_menu")
public class Menu implements Serializable
{
	private static final long serialVersionUID = 3535301802680517238L;

	@Id
	@GeneratedValue(generator = "system-uuid")
	@GenericGenerator(name = "system-uuid", strategy = "uuid.hex")
	@Column(name = "ID")
	@Access(AccessType.PROPERTY)
	private String id;

	@Column(name = "menuname")
	private String menuname;

	@Column(name = "url")
	private String url;
	
	@Column(name = "menu_code")
	private String menuCode;

	@ManyToOne(optional = true)
	@JoinColumn(name = "pid")
	private Menu parent;

	@OneToMany(mappedBy = "parent",fetch = FetchType.LAZY)
	private Set<Menu> children = new HashSet<Menu>();
	
	@OneToMany(fetch=FetchType.LAZY,cascade=CascadeType.REMOVE,mappedBy="menu")
	private Set<Function> funcs;
	
	@ManyToMany(fetch=FetchType.LAZY,mappedBy="menus")
	private Set<Role> roles;

	@Override
	public boolean equals(Object obj)
	{
		if(obj instanceof Menu){
			Menu menu = (Menu)obj;
			if(menu.getId().equals(this.getId())){
				return true;
			}
		}
		
		return false;
	}
	
	@Override
	public int hashCode()
	{
		return this.id.hashCode();
	}

	public String getId()
	{
		return id;
	}

	public void setId(String id)
	{
		this.id = id;
	}

	public String getMenuname()
	{
		return menuname;
	}

	public void setMenuname(String menuname)
	{
		this.menuname = menuname;
	}

	public String getUrl()
	{
		return url;
	}

	public void setUrl(String url)
	{
		this.url = url;
	}

	public Menu getParent()
	{
		return parent;
	}

	public void setParent(Menu parent)
	{
		this.parent = parent;
	}

	public Set<Menu> getChildren()
	{
		return children;
	}

	public void setChildren(Set<Menu> children)
	{
		this.children = children;
	}

	public String getMenuCode()
	{
		return menuCode;
	}

	public void setMenuCode(String menuCode)
	{
		this.menuCode = menuCode;
	}

	public Set<Function> getFuncs()
	{
		return funcs;
	}

	public void setFuncs(Set<Function> funcs)
	{
		this.funcs = funcs;
	}

	public Set<Role> getRoles()
	{
		return roles;
	}

	public void setRoles(Set<Role> roles)
	{
		this.roles = roles;
	}
}
